/**
 * 项目名称: Topic_113.cpp
 * 项目路径: study/DataStructure/LeetCode/树/中等
 * 编辑时间: 2024年08月16日08时56分26秒
 * 用   户: 轻尘
 */

#include <bits/stdc++.h>

using namespace std;

struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;

    TreeNode() : val(0), left(nullptr), right(nullptr) {}

    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}

    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};

class Solution {
public:
    vector<vector<int>>ans;
    int getAdd(vector<int>way){
        int sum =0 ;
        for(auto x:way)sum+=x;
        return sum;
    }
    void pre(TreeNode* root,int targetSum,vector<int>way){
        if(!root)return;
        way.push_back(root->val);
        pre(root->left,targetSum,way);
        pre(root->right,targetSum,way);
        if(!root->left && !root->right){
            if(targetSum == getAdd(way)){
                ans.push_back(way);
                return;
            }
        }
    }
    vector<vector<int>> pathSum(TreeNode* root, int targetSum) {
        if(!root)return ans;
        vector<int>way;
        pre(root,targetSum,way);
        return ans;
    }
};
